package com.example.springboot.service.impl;

import com.example.springboot.common.request.PageRequest;
import com.example.springboot.common.dto.LoginDTO;
import com.example.springboot.common.exception.ServiceException;
import com.example.springboot.common.request.LoginRequest;
import com.example.springboot.common.request.PasswordRequest;
import com.example.springboot.common.request.SetNewPasswordRequest;
import com.example.springboot.common.result.Result;
import com.example.springboot.common.utils.JasyptEncryptorUtils;
import com.example.springboot.common.utils.TokenUtils;
import com.example.springboot.entity.Admin;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.example.springboot.common.exception.ExceptionConstant.Exists_Username_ERROR;
import static com.example.springboot.common.exception.ExceptionConstant.Password_Duplication_ERROR;


@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 管理员登录
     * @param loginRequest
     * @return
     */
    public LoginDTO login(LoginRequest loginRequest) {
        // 先通过用户名寻找加密密码
        Admin admin = adminMapper.getByUsername(loginRequest.getUsername());

        // 接着对找到的密码解密
        String plainPassword = JasyptEncryptorUtils.decode(admin.getPassword());

        if(admin == null || !plainPassword.equals(loginRequest.getPassword())) {
            throw new ServiceException("用户名或密码错误!");
        }
        if(!admin.isStatus()) {
            throw new ServiceException("当前用户处于禁用状态，请联系管理员!");
        }

        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(admin, loginDTO);

        // 生成token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());
        loginDTO.setToken(token);

        return loginDTO;
    }

    /**
     * 分页模糊查询
     * @param pageRequest
     * @return
     */
    public PageInfo<Admin> page(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Admin> admins = adminMapper.listByCondition(pageRequest);
        return new PageInfo<>(admins);
    }

    /**
     * 添加管理员
     * @param admin
     * @throws Exception
     */
    public void save(Admin admin) throws Exception {
        // 判断用户名是否已存在
        if(adminMapper.findAdminByUsername(admin.getUsername()) != null) {
            throw new ServiceException(Exists_Username_ERROR);
        }

        // 密码加密
        String plainPassword = admin.getPassword();
        String encryptedPassword = JasyptEncryptorUtils.encode(plainPassword);
        admin.setPassword(encryptedPassword);

        admin.setUpdateTime(new Date());
        adminMapper.save(admin);
    }

    /**
     * 根据id获取需要修改管理员的信息
     * @param id
     * @return
     */
    public Admin getById(Integer id) {
        Admin admin = adminMapper.getById(id);
        return admin;
    }

    /**
     * 更新管理员信息
     * @param admin
     * @throws Exception
     */
    public void update(Admin admin) throws Exception {
        // 判断管理员用户名是否已经存在
        if(adminMapper.findAdminByUsernameAndId(admin) != null) {
            throw new ServiceException(Exists_Username_ERROR);
        }

        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
    }

    /**
     * 修改密码
     * @param passwordRequest
     */
    public void changePassword(PasswordRequest passwordRequest) {
        // 判断新密码是否与之前的一致
        if(passwordRequest.getPassword().equals(passwordRequest.getNewPassword())) {
            throw new ServiceException(Password_Duplication_ERROR);
        }

        // 密码加密
        String plainPassword = passwordRequest.getNewPassword();
        String encryptedPassword = JasyptEncryptorUtils.encode(plainPassword);
        passwordRequest.setNewPassword(encryptedPassword);

        adminMapper.changePassword(passwordRequest.getId(), passwordRequest.getNewPassword());
    }

    /**
     * 根据用户名重置新密码
     * @param request
     */
    public void setPasswordByUsername(SetNewPasswordRequest request) {
        adminMapper.setPasswordByUsername(request);
    }

    /**
     * 修改管理员状态
     * @param admin
     */
    public void updateStatus(Admin admin) {
        // 密码加密
        String plainPassword = admin.getPassword();
        String encryptedPassword = JasyptEncryptorUtils.encode(plainPassword);
        admin.setPassword(encryptedPassword);

        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
    }

    /**
     * 根据id删除管理员
     * @param id
     */
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }



}
